import { Context } from 'koa';
const { getRoleList, getRoleListByPageInfo, searchRolesByname, getPermissionByRoles, getRoleByIdentity, creatRole, updateRoleInfo, deleteById } = require('../service/role.service')
const { invalidID } = require('../constants/err.type')

interface pageInfo {
    pageNum?: number,
    pageSize?: number
}

class RoleController {
    // 获取权限身份接口
    public async getRoles(ctx: Context) {
        try {
            let res: any = null;
            let { pageNum, pageSize }: pageInfo = ctx.query;
            if (pageNum && pageSize) {
                pageNum = Math.max(pageNum * 1, 1);
                pageSize = Math.max(pageSize * 1, 1);
                res = await getRoleListByPageInfo(pageNum, pageSize);
            } else {
                res = await getRoleList();
            }
            ctx.body = {
                code: 200,
                message: '获取角色列表成功',
                result: {
                    ...res
                }
            }
        } catch (error) {
            console.error('获取角色列表失败', error)
        }
    }
    // 根据名字获取角色
    public async getRolesByname(ctx: Context) {
        try {
            const name = ctx.query.name;
            const pageNum = ctx.query.pageNum
            const pageSize = ctx.query.pageSize
            const res = await searchRolesByname({ name, pageNum, pageSize })
            ctx.body = {
                code: 200,
                message: '搜索角色成功',
                result: res
            }
        } catch (error) {
            console.error('根据名字获取角色列表失败', error)
        }
    }
    // 获取角色对应的权限
    public async getPermissions(ctx: Context) {
        try {
            const res = await getPermissionByRoles()
            ctx.body = {
                code: 200,
                message: '获取角色对应的权限成功',
                result: res
            }
        } catch (error) {
            console.error('获取角色对应的权限列表失败', error)
        }
    }
    // 根据id获取角色
    public async getRoleById(ctx: Context) {
        try {
            const identity = ctx.params.identity
            const res = await getRoleByIdentity(identity)
            if(!res) {
                return ctx.app.emit('error', invalidID, ctx)
            }
            ctx.body = {
                code: 200,
                message: '根据id获取角色成功',
                result: res
            }
        } catch (error) {
            console.error('根据id获取角色失败', error)
        }
    }
    // 添加角色
    public async addRole(ctx: Context) {
        try {
            const res = await creatRole(ctx.request.body);
            ctx.body = {
                code: 200,
                message: `添加角色${ctx.request.body.role_name}成功`
            }
        } catch (error) {
            console.error('添加角色失败', error)
        }
    }
    // 更新角色
    public async updateRole(ctx: Context) {
        try {
            const identity = ctx.params.identity
            const res = await updateRoleInfo(identity, ctx.request.body)
            if(res) {
                ctx.body = {
                    code: 200,
                    message: `修改角色${ctx.request.body.role_name}成功`
                }
            }
        } catch (error) {
            console.error('更新角色失败', error)
        }
    }
    // 删除角色
    public async deleteRole(ctx: Context) {
        try {
            const identity = ctx.params.identity
            const res = await deleteById(identity)
            if(res) {
                ctx.body = {
                    code: 200,
                    message: '删除用户成功'
                }
            } else {
                ctx.body = {
                    code: 500,
                    message: '删除用户失败'
                }
            }
        } catch (error) {
            console.error('删除角色失败', error)
        }
    }
}

module.exports = new RoleController();